package com.software.chemistry.service.Impl;

import com.software.chemistry.entity.StudentInfo;
import com.software.chemistry.entity.TeacherInfo;
import com.software.chemistry.entity.UserQuery;
import com.software.chemistry.repository.TeacherInfoRepositiory;
import com.software.chemistry.service.TeacherInfoService;
import com.software.chemistry.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class TeacherInfoServiceImpl implements TeacherInfoService {
    @Autowired
    private TeacherInfoRepositiory teacherInfoRepositiory;
    @Override
    public TeacherInfo findTeacherInfoByuserId(String userId) {
        Optional<TeacherInfo> optional = teacherInfoRepositiory.findById(userId) ;
        TeacherInfo teacherInfo = optional.get() ;
        return teacherInfo ;
    }

    @Override
    public List<TeacherInfo> findTeacherInfos() {
        List<TeacherInfo> teacherInfos = teacherInfoRepositiory.findAll() ;
        return teacherInfos ;
    }

    @Override
    public TeacherInfo createTeacherInfo(TeacherInfo teacherInfo) {
        String md5data= SecurityUtil.md5String(teacherInfo.getPassword());
        String md5Password=SecurityUtil.upperCaseMd5(md5data);
        teacherInfo.setPassword(md5Password);
        TeacherInfo save= teacherInfoRepositiory.save(teacherInfo);
        return save;
    }

    @Override
    public List<TeacherInfo> findTeacherInfoByUserName(String param_username) {
        return teacherInfoRepositiory.findTeacherInfoByUserName(param_username);
    }

    @Override
    public List<TeacherInfo> verifyLoginUser(String userName, String password) {
        String md5data= SecurityUtil.md5String(password);
        String md5Password=SecurityUtil.upperCaseMd5(md5data);
        List<TeacherInfo> teacherInfo= teacherInfoRepositiory.findTeacherByUserNameAndPassword(userName,md5Password);
        return teacherInfo;
    }

    @Override
    public List<UserQuery> findTeacherInfoSByUserNameOrUserId(String str, String str1) {
        List<TeacherInfo> teacherInfos = teacherInfoRepositiory.findTeacherInfosByUserIdOrUserName(str, str1) ;
        List<UserQuery> userQueries = new ArrayList<>() ;
        for (int i = 0; i < teacherInfos.size(); i ++ ) {
            UserQuery userQuery = new UserQuery() ;
            userQuery.setUserName(teacherInfos.get(i).getUserName());
            userQuery.setIcon(teacherInfos.get(i).getIcon());
            userQuery.setUserId(teacherInfos.get(i).getUserId());
            userQueries.add(userQuery) ;
        }
        return userQueries ;
    }


}
